package com.xps.charmingshop.view.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jude.utils.JUtils;
import com.xps.charmingshop.R;
import com.xps.charmingshop.adapter.OrderDetailsListAdapter;
import com.xps.charmingshop.bean.OrderDetailsBean;
import com.xps.charmingshop.bean.VerificationBean;
import com.xps.charmingshop.date.net.UserNetWorks;
import com.xps.charmingshop.utils.SharedPreferencesUtils;
import com.xps.charmingshop.view.base.BaseActivity;
import com.xps.charmingshop.view.custom.WheelView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by MI on 2018/1/29.
 */

public class OrderDetailsActivity extends BaseActivity {
    private static final String[] PLANETS = new String[]{"10", "20", "30", "40", "50", "60"};

    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.actionbar_tv_title)
    TextView actionbarTvTitle;
    @BindView(R.id.elv_collocation)
    ExpandableListView elvCollocation;
    @BindView(R.id.Text_Money)
    TextView TextMoney;
    @BindView(R.id.Text_Remarks)
    TextView TextRemarks;
    @BindView(R.id.Text_Subscribe)
    TextView TextSubscribe;
    @BindView(R.id.Text_Choice)
    TextView TextChoice;
    @BindView(R.id.rll_timeoice)
    RelativeLayout rllChoice;
    String ste;
    Intent intent;
    List<OrderDetailsBean.ReturnBodyBean.GoodsListBean> returnBodyBeen;
    List<OrderDetailsBean.ReturnBodyBean.GoodsListBean.GoodsMustOptionalBean> dishInfoBeen;
    @BindView(R.id.Text_TimeToStore)
    TextView TextTimeToStore;
    private TextView tvOrderState;

    @Override
    protected int getLayout() {
        return R.layout.activity_orderdetails;
    }

    @Override
    protected void setData() {
        actionbarTvTitle.setText("订单详情");
        actionbarTvTitle.setTextColor(getResources().getColor(R.color.white));

    }

    @Override
    protected void init(Bundle inBundle) {
        ButterKnife.bind(this);
        intent = getIntent();
        getOrderDetails();
        tvOrderState = (TextView) findViewById(R.id.tv_orderstate);

        elvCollocation.setSelector(new ColorDrawable(Color.TRANSPARENT));
    }

    @OnClick({R.id.actionbar_iv_back, R.id.actionbar_tv_title, R.id.Text_Subscribe, R.id.rll_timeoice})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.actionbar_iv_back:
                finish();
                break;
            case R.id.rll_timeoice:
                showCustomizeDialog(PLANETS);
                break;

        }
    }

    public void showCustomizeDialog(String[] PLANETS) {
        View outerView = LayoutInflater.from(OrderDetailsActivity.this).inflate(R.layout.wheel_view_xin, null);
        WheelView wv = (WheelView) outerView.findViewById(R.id.picker_rel);
        wv.setOffset(2);
        wv.setItems(Arrays.asList(PLANETS));
        wv.setSeletion(0);
        wv.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                ste = item;
            }
        });
        wv.onSeletedCallBack();
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(OrderDetailsActivity.this);
        alertDialog.setTitle("请选择配菜时间");
        alertDialog.setView(outerView);
        alertDialog.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                getUpdateDate(ste);
                TextChoice.setText(ste);
            }
        });
        alertDialog.show();
    }

    public void getOrderDetails() {
        String user_token = SharedPreferencesUtils.getString("user_token", "String", null);
        UserNetWorks.getOrderDetails(user_token, intent.getStringExtra("getOrder_id"), new Subscriber<OrderDetailsBean>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(OrderDetailsBean orderDetailsBean) {
                if (orderDetailsBean.getReturn_code().equals("SUCCESS")) {
                    returnBodyBeen = new ArrayList<>();
                    dishInfoBeen = new ArrayList<>();
                    List<OrderDetailsBean.ReturnBodyBean.GoodsListBean> list = orderDetailsBean.getReturn_body().getGoods_list();
                    TextMoney.setText(orderDetailsBean.getReturn_body().getGoods_money_total());
                    TextRemarks.setText(orderDetailsBean.getReturn_body().getOrder_remarks());
                    if (orderDetailsBean.getReturn_body().getOrder_status().equals("-1")) {
                        tvOrderState.setText("未处理");
                    } else if (orderDetailsBean.getReturn_body().getOrder_status().equals("1")) {
                        tvOrderState.setText("准备中");
                    } else if (orderDetailsBean.getReturn_body().getOrder_status().equals("2")) {
                        tvOrderState.setText("配送中");
                    } else if (orderDetailsBean.getReturn_body().getOrder_status().equals("3")) {
                        tvOrderState.setText("已完成");
                    } else if (orderDetailsBean.getReturn_body().getOrder_status().equals("4")) {
                        tvOrderState.setText("已取消");
                    }
                    if (!orderDetailsBean.getReturn_body().getReady_dish_time().equals("0")) {
                        rllChoice.setEnabled(false);
                        TextChoice.setText(orderDetailsBean.getReturn_body().getReady_dish_time());
                    }
                    if (!orderDetailsBean.getReturn_body().getTime_to_shop().equals("")) {
                        parseTime(orderDetailsBean.getReturn_body().getTime_to_shop(), TextTimeToStore);
                    } else {
                        TextTimeToStore.setText("未设置到店时间");
                    }
                    for (int i = 0; i < list.size(); i++) {
                        OrderDetailsBean.ReturnBodyBean.GoodsListBean bean = new OrderDetailsBean.ReturnBodyBean.GoodsListBean();
                        bean.setDish_name(list.get(i).getDish_name());
                        bean.setGoods_id(list.get(i).getGoods_id());
                        bean.setDish_price(list.get(i).getDish_price());
                        bean.setGoods_num(list.get(i).getGoods_num());
                        bean.setGoods_dish_price(list.get(i).getGoods_dish_price());
                        bean.setGoods_remarks(((Float.valueOf(list.get(i).getDish_price()) - Float.valueOf(list.get(i).getDish_discount_price())) * Float.valueOf(list.get(i).getGoods_num())) + "");
                        //分类里的菜品
                        List<OrderDetailsBean.ReturnBodyBean.GoodsListBean.GoodsMustOptionalBean> list1 = list.get(i).getGoods_must_optional();
                        String img = null;
                        for (int k = 0; k < list1.size(); k++) {
                            OrderDetailsBean.ReturnBodyBean.GoodsListBean.GoodsMustOptionalBean bean1 = new OrderDetailsBean.ReturnBodyBean.GoodsListBean.GoodsMustOptionalBean();
                            bean1.must_name = list1.get(k).getMust_name();
                            bean1.must_price = list1.get(k).getMust_price();
                            bean1.optional_name = list1.get(k).getOptional_name();
                            bean1.optional_price = list1.get(k).getOptional_price();
                            dishInfoBeen.add(bean1);
                        }
                        returnBodyBeen.add(bean);
                        //添加每一种类型的菜品
                        bean.setGoods_must_optional(list1);
                        elvCollocation.setAdapter(new OrderDetailsListAdapter(OrderDetailsActivity.this, elvCollocation, returnBodyBeen));
                        elvCollocation.expandGroup(0);//默认展开第一个
                    }

                } else if (orderDetailsBean.getReturn_code().equals("FAIL")) {
                    JUtils.Toast(orderDetailsBean.getReturn_msg());
                }
            }
        });
    }

    /**
     * 解析时间
     */
    void parseTime(String createTime, TextView tvOrderTime) {
        long lTime = Long.valueOf(createTime);
        long nTime = lTime * 1000;
        Date passDate = new Date(nTime);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH点mm分");
        tvOrderTime.setText(simpleDateFormat.format(passDate));
    }


    public void getUpdateDate(String ste) {
        String user_token = SharedPreferencesUtils.getString("user_token", "String", null);
        UserNetWorks.getUpdateDate(user_token, intent.getStringExtra("getOrder_id"), ste, new Subscriber<VerificationBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(VerificationBean verificationBean) {
                if (verificationBean.getReturn_code().equals("SUCCESS")) {
                    JUtils.Toast(verificationBean.getReturn_msg());
                    rllChoice.setEnabled(false);
                    rllChoice.setClickable(false);
                    getOrderDetails();
                } else if (verificationBean.getReturn_code().equals("FAIL")) {
                    JUtils.Toast(verificationBean.getReturn_msg());
                }
            }
        });

    }

}
